package utility;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Rect;

public class Sprite {
	public static final int ORIENTED_VERTICAL = 1;
	public static final int ORIENTED_HORIZONTAL = 2;

	protected Bitmap sprite;
	protected int xPost;
	protected int yPost;

	protected Rect frame;

	protected int timelifeFrame;
	protected int numOFrame;
	protected int currentFrame;
	protected long time;

	protected int frameHeight;
	protected int frameWidth;

	protected boolean loop;
	protected boolean invisible;
	protected boolean dispose;
	protected int delay;
	protected int oriented;

	public Sprite() {
		this.oriented = ORIENTED_HORIZONTAL;
		this.delay = 0;
		this.xPost = 0;
		this.yPost = 0;
		this.time = 0;
		this.currentFrame = 0;
		this.dispose = false;
		this.frame = new Rect(0, 0, 0, 0);

	}

	public void Initialize(Bitmap bitmap, int frameHeight, int frameWidth,
			int fps, int numOFrame, boolean loop) {
		this.sprite = bitmap;
		this.frameHeight = frameHeight;
		this.frameWidth = frameWidth;
		this.frame.top = 0;
		this.frame.left = 0;
		this.frame.bottom = this.frameHeight;
		this.frame.right = this.frameWidth;
		this.timelifeFrame = 1000 / fps;
		this.loop = loop;
		this.invisible = false;
		this.numOFrame = numOFrame;
		this.time = System.currentTimeMillis() + this.getDelay();
	}

	public int getOriented() {
		return oriented;
	}

	public void setOriented(int oriented) {
		this.oriented = oriented;
	}

	public boolean isInvisible() {
		return invisible;
	}

	public void setInvisible(boolean invisible) {
		this.invisible = invisible;
	}

	public boolean isDispose() {
		return dispose;
	}

	public int getDelay() {
		return delay;
	}

	public void setDelay(int delay) {
		this.delay = delay;
	}

	public int getFrameHeight() {
		return frameHeight;
	}

	public int getFrameWidth() {
		return frameWidth;
	}

	public int getxPost() {
		return xPost;
	}

	public int getyPost() {
		return yPost;
	}

	public void setxPost(int xPost) {
		this.xPost = xPost - frameWidth / 2;
	}

	public void setyPost(int yPost) {
		this.yPost = yPost - frameHeight / 2;
	}

	public Bitmap getSprite() {
		return sprite;
	}

	public void setSprite(Bitmap sprite) {
		this.sprite = sprite;
	}

	public void Update(Long gameTime) {
		if (gameTime <= this.time && !this.loop) {
			currentFrame += 1;
			if (currentFrame >= numOFrame) {
				currentFrame = 0;
			}
		}

		if (gameTime >= this.time + timelifeFrame) {
			this.time = gameTime;
			currentFrame += 1;
			if (currentFrame >= numOFrame) {
				currentFrame = 0;
				if (!this.loop)
					this.dispose = true;
			}
		}
		switch (this.oriented) {
		case ORIENTED_VERTICAL:
			this.frame.top = currentFrame * frameHeight;
			this.frame.bottom = this.frame.top + frameHeight;
			break;
		case ORIENTED_HORIZONTAL:
			this.frame.left = currentFrame * frameWidth;
			this.frame.right = this.frame.left + frameWidth;
		default:
			break;
		}

	}

	public void draw(Canvas canvas) {
		Rect rec = new Rect(this.getxPost(), this.getyPost(), this.getxPost()
				+ this.frameWidth, this.getyPost() + this.frameHeight);
		canvas.drawBitmap(this.sprite, this.frame, rec, null);
	}
}